package com.epfresh.supplier.activity;


import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.epfresh.api.dialog.TimePickerDialog;
import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.ucrop.PhotoCore;
import com.epfresh.api.utils.CashierInputFilter;
import com.epfresh.api.utils.DateUtil;
import com.epfresh.api.widget.CommonInputItem;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.delegate.UploadProductDelegate;
import com.epfresh.supplier.entity.PurchaseInfo;
import com.epfresh.supplier.entity.StoreInfo;
import com.google.gson.Gson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class ProductAddStockHomeActivity  extends BaseActivity {

    private CommonInputItem ciProductDate;
    private CommonInputItem ciProductDateLine;
    private CommonInputItem ciProductOrigin;
    private CommonInputItem ciProductMarket;
    private CommonInputItem ciProductStore;
    private CommonInputItem ciProductInDate;
    private CommonInputItem ciProductInDetail;
    private EditText edNumberInput;
    private TextView tvNumberInventory;

    private EditText edPriceInput;
    private TextView tvPriceUnit;

    private TextView tvBtn;
    private UploadProductDelegate upd;
    private PhotoCore photoCoreDetail;
    private PurchaseInfo pi ;
    private String chargeUnit ;
    private String productId;
    //1,现货;2,预售;
    String type;
    String orderId;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("productId",getIntent().getStringExtra("productId"));
        outState.putString("PurchaseInfo",getIntent().getStringExtra("PurchaseInfo"));
        outState.putString("type",getIntent().getStringExtra("type"));
        outState.putString("chargeUnit",getIntent().getStringExtra("chargeUnit"));
        outState.putString("orderId",getIntent().getStringExtra("orderId"));
        outState.putBoolean("isEdit",getIntent().getBooleanExtra("isEdit",false));
        outState.putString("price",getIntent().getStringExtra("price"));
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_stock);
        if (savedInstanceState != null) {
            getIntent().putExtra("productId", savedInstanceState.getString("productId"));
            getIntent().putExtra("PurchaseInfo", savedInstanceState.getString("PurchaseInfo"));
            getIntent().putExtra("type", savedInstanceState.getString("type"));
            getIntent().putExtra("chargeUnit", savedInstanceState.getString("chargeUnit"));
            getIntent().putExtra("orderId", savedInstanceState.getString("orderId"));
            getIntent().putExtra("isEdit", savedInstanceState.getBoolean("isEdit",false));
            getIntent().putExtra("price",getIntent().getStringExtra("price"));
        }
        orderId = getIntent().getStringExtra("orderId");
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("进货信息");
        rightbtn.setText(getString(R.string.help));
        rightbtn.setTextColor(0xff898989);
        rightbtn.setVisibility(View.GONE);
//        rightbtn.setOnClickListener(this);

        initView();

    }

    @Override
    public void onRightClick() {
        Intent intent = new Intent(this, WebActivity.class);
        StoreInfo storeInfo = getStoreInfo();
        if (storeInfo != null && storeInfo.isDc()) {
            intent.putExtra(Constant.KEY_WEB_URL, Constant.URL_HELP_PRODUCT_DC);
        } else {
            intent.putExtra(Constant.KEY_WEB_URL, Constant.URL_HELP_PRODUCT);

        }
        intent.putExtra(Constant.KEY_WEB_TITLE, getString(R.string.help));
        startActivity(intent);
    }

    private void initView() {

        ciProductDate = (CommonInputItem) findViewById(R.id.ci_product_date);
        ciProductDateLine = (CommonInputItem) findViewById(R.id.ci_dateline);
        ciProductOrigin = (CommonInputItem) findViewById(R.id.ci_product_origin);
        ciProductMarket = (CommonInputItem) findViewById(R.id.ci_market);
        ciProductStore = (CommonInputItem) findViewById(R.id.ci_store);
        ciProductInDate = (CommonInputItem) findViewById(R.id.ci_in_date);
        ciProductInDetail = (CommonInputItem) findViewById(R.id.ci_product_detail);

        edNumberInput = (EditText) findViewById(R.id.ed_inventory_input);
        edPriceInput = (EditText) findViewById(R.id.ed_price_input);

        CashierInputFilter cashierInputFilter = new CashierInputFilter();
        cashierInputFilter.MAX_VALUE = 9999999;
        edNumberInput.setFilters(new InputFilter[]{cashierInputFilter});
        edPriceInput.setFilters(new InputFilter[]{cashierInputFilter});

        tvNumberInventory = (TextView) findViewById(R.id.tv_inventory_unit);
        tvPriceUnit = (TextView) findViewById(R.id.tv_price_unit);
        tvBtn = (TextView) findViewById(R.id.tv_btn);
        TextView tvInventoryTitle = (TextView) findViewById(R.id.tv_inventory_title);
        String s = "重量/数量 <font color=\"#ff0000\">*</font>";
        tvInventoryTitle.setText(Html.fromHtml(s));

        tvBtn.setOnClickListener(this);

        ciProductDate.setOnClickListener(this);
        ciProductInDate.setOnClickListener(this);
        ciProductDateLine.setOnClickListener(this);
        upd = new UploadProductDelegate(this);
        if (photoCoreDetail == null) {
            photoCoreDetail = upd.getPhotoCore();
        }


//        ProductAddEntity pae = DataManager.getInstance().getProductAddEntity();
        type = getIntent().getStringExtra("type");
        chargeUnit = getIntent().getStringExtra("chargeUnit");


        String purchaseInfo = getIntent().getStringExtra("PurchaseInfo");
        if (purchaseInfo != null && !"".equals(purchaseInfo)){
            pi =  new Gson().fromJson(purchaseInfo,PurchaseInfo.class);
        }

        if (pi == null){
            pi = new PurchaseInfo();
        }



        if ("1".equals(type)){//现货
            ciProductDateLine.setVisibility(View.GONE);
            ciProductInDate.setTitleText("进货日期");
            String arrivalDate = pi.getArrivalDate();
            if (arrivalDate == null || "".equals(arrivalDate)) {
                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String date = sDateFormat.format(new Date());
                pi.setArrivalDate(date);
            }
            if(pi.getProductionDate()==null||"".equals(pi.getProductionDate()))
                pi.setProductionDate(pi.getArrivalDate());

        }else {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = sDateFormat.format(new Date());
            pi.setProductionDate(date);
            ciProductInDate.setNotNull(true);
            ciProductInDate.setTitleText("预计送达时间");
//            ciProductDateLine.setVisibility(View.VISIBLE);
            ciProductInDate.setVisibility(View.GONE);
            ciProductDateLine.setVisibility(View.GONE);

        }
        productId = getIntent().getStringExtra("productId");
        initData();
    }


    private void initData(){
        ciProductDate.setContentText(pi.getProductionDate());
        ciProductInDate.setContentText(pi.getArrivalDate());
        //if(pi)
        ciProductDateLine.setContentText(pi.getSaleDeadline());
        ciProductOrigin.setContentText(pi.getOrigin());
        ciProductMarket.setContentText(pi.getUpstreamMarket());
        ciProductStore.setContentText(pi.getUpstreamMerchant());
//        edNumberInput.setText(pi.getQuantity() + "");
//        edNumberInput.setSelection(edNumberInput.length());
        tvNumberInventory.setText(chargeUnit);

        edPriceInput.setText(pi.getPrice());
        edPriceInput.setSelection(edPriceInput.length());
        tvPriceUnit.setText("元/"+chargeUnit);
        if(chargeUnit != null) {
            tvPriceUnit.setText("元/" + chargeUnit);
            tvPriceUnit.setMinEms(2);
            tvNumberInventory.setMinEms(2);
        }

        if (orderId != null && !"".equals(orderId)){
            ciProductOrigin.setEditable(false);
            ciProductStore.setEditable(false);
        }
//        upd.setImgList(pi.getImages());
        upd.setImgList(pi.getOriginProveImages());

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);

        switch (view.getId()){
            case R.id.ci_product_date:
                String t1 = ciProductDate.getContentText();
                openProductDate(t1);
                break;

            case R.id.ci_in_date:
                String t2 = ciProductInDate.getContentText();
                openInDate(t2);
                break;
            case R.id.ci_dateline:
                String t3 = ciProductDateLine.getContentText();
                openDateline(t3);
                break;
            case R.id.tv_btn:
                save();
                break;
        }
    }

    private boolean isCanSave(){
//        if (ciProductDate.isContentEmpty()){
//            toast("请选择生产日期");
//            return;
//        }
//        if (ciProductOrigin.isContentEmpty()){
//            toast("请输入您商品的产地或生产基地");
//            return;
//        }
//        if (ciProductMarket.isContentEmpty()){
//            toast("请输入您商品的上游批发市场");
//            return;
//        }
//        if (ciProductStore.isContentEmpty()){
//            toast("请输入您商品的上游商户");
//            return;
//        }
        if (TextUtils.isEmpty(edNumberInput.getText())){
            toast("请填写重量/数量");
            return false;
        }

        if ("1".equals(type)){//现货

        }else {
//            if (ciProductInDate.isContentEmpty()){
//                toast("请选择预计送达时间");
//                return false;
//            }
//            if (ciProductDateLine.isContentEmpty()){
//                toast("请选择预售截止时间");
//                return false;
//            }
        }

        return true;
    }

    private void save() {
        if (!isCanSave()){
            return;
        }

        String productDate = ciProductDate.getContentText();
        String arrivalDate = ciProductInDate.getContentText();
        String market = ciProductMarket.getContentText();
        String upstreamMerchant = ciProductStore.getContentText();
        String origin = ciProductOrigin.getContentText();
        String deadline = ciProductDateLine.getContentText();
        String quantity = edNumberInput.getText().toString();
        String price = edPriceInput.getText().toString();

        if ("1".equals(type)) {//现货

            if (!isDateLegal(arrivalDate, productDate)) {
                return;
            }

        } else {
           /* if (!isPreDateLegal(arrivalDate, deadline, productDate)) {
                return;
            }*/
        }
        pi.setArrivalDate(arrivalDate);
        pi.setProductionDate(productDate);
        pi.setUpstreamMarket(market);
        pi.setUpstreamMerchant(upstreamMerchant);
        pi.setOrigin(origin);
        pi.setQuantity(Double.valueOf(quantity));
        pi.setPrice(price);
        pi.setOriginProveImageIds(upd.getImagesStr());
        pi.setImages(upd.getImages());

        if ("1".equals(type)){//现货
            pi.setSaleDeadline(null);
        }else {
            pi.setSaleDeadline(deadline);
        }
        RequestEntityMapSupply re = new RequestEntityMapSupply();

        re.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_product_purchase);
        re.putParameter("productSpecId", productId);
        re.putParameter("price", price);
        re.putParameter("arrivalDate", arrivalDate);
        re.putParameter("productionDate", productDate);
        re.putParameter("origin", origin);
        re.putParameter("upstreamMarket", market);
        re.putParameter("upstreamMerchant", upstreamMerchant);
        re.putParameter("quantity", quantity);
        re.putParameter("originProveImageIds", upd.getImagesStr());
        request(re, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_product_purchase, onPurchaseAddRequestListener);

/*        boolean isEdit = getIntent().getBooleanExtra("isEdit", false);

        if (hasProductId()) {
            if (isEdit){
                Intent intent = new Intent();
                intent.putExtra("result", new Gson().toJson(pi));
                setResult(RESULT_OK, intent);
                finish();
            }else {
                pi.setSpecId(productId);
                requestProductModify();
            }
        } else {
            Intent intent = new Intent();
            intent.putExtra("result", new Gson().toJson(pi));
            setResult(RESULT_OK, intent);

            finish();
        }*/

    }

    public boolean isDateLegal(String arrivalDate,String productDate) {

        //预计送达时间

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = new Date(System.currentTimeMillis());
            String today = sdf.format(date);

            int c = DateUtil.compare(arrivalDate, productDate);
            if (c == -1 || c == 0) {
                c = DateUtil.compare(today, productDate);
                if (c == -1 || c == 0) {
                    c = DateUtil.compare(today, arrivalDate);
                    if (c == -1 || c == 0) {

                        return true;
                    }else {
                        toast("进货日期不能早于今天");
                    }
                }else {
                    toast("生产日期不能早于今天");
                }
            }else {
                toast("进货日期不能早于生产日期");
                return false;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    private void openInDate(String time) {
        TimePickerDialog timeDialog = new TimePickerDialog(this);



        timeDialog.setOnPositiveClickListener(new TimePickerDialog.OnPositiveClickListener() {
            @Override
            public void onPositiveClickListener(int index, String data) {
                ciProductInDate.setContentText(data);
            }
        });

        if ("1".equals(type)) {//现货
            calculateInDate(timeDialog);
            timeDialog.setTitle("进货日期");
        }else {//预售
            timeDialog.setTitle("预计送达时间");
            calculatePreInDate(timeDialog);
        }
        timeDialog.setCurrentDate(time);

        timeDialog.show();
    }

    //现货进货日期约束
    private void calculateInDate(TimePickerDialog timeDialog) {
        String productDate = ciProductDate.getContentText();

        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            //截止时间
            if (productDate != null && !"".equals(productDate)) {
                long timeMill = sdf.parse(productDate).getTime();
                calendar.setTimeInMillis(timeMill);

                int sy = calendar.get(Calendar.YEAR);
                int sm = calendar.get(Calendar.MONTH) + 1;
                int sd = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);

//                calendar.add(Calendar.MONTH,24);
                calendar.setTimeInMillis(System.currentTimeMillis());
                int ey = calendar.get(Calendar.YEAR);
                int em = calendar.get(Calendar.MONTH) + 1;
                int ed = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setEndYear(ey);
                timeDialog.setEndMonth(em);
                timeDialog.setEndDay(ed);
                if (ey < sy) {
                    sy = ey;
                }
                if (sy == ey && em < sm) {
                    sm = em;
                }
                if (sy == ey && em == sm && ed < sd) {
                    sd = ed;
                }
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);

            } else {
                calendar.setTimeInMillis(System.currentTimeMillis());
//                calendar.add(Calendar.MONTH,24);

                int ey = calendar.get(Calendar.YEAR);
                int em = calendar.get(Calendar.MONTH) + 1;
                int ed = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setEndYear(ey);
                timeDialog.setEndMonth(em);
                timeDialog.setEndDay(ed);
                calendar.add(Calendar.MONTH,-24);
                int sy = calendar.get(Calendar.YEAR);
                int sm = calendar.get(Calendar.MONTH) + 1;
                int sd = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
                if (ey < sy) {
                    sy = ey;
                }
                if (sy == ey && em < sm) {
                    sm = em;
                }
                if (sy == ey && em == sm && ed < sd) {
                    sd = ed;
                }
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
            }



        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
    private void calculatePreInDate(TimePickerDialog timeDialog) {

        String productDate = ciProductDate.getContentText();
        String productDateLine = ciProductDateLine.getContentText();

        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {

            String time = null;
            int c = DateUtil.compare(productDate,productDateLine);
            // -3,time1,time2都为空，1,time2大，-1,time1大，0,一样大 ，-2,异常
            if (c == 1){
                time  = productDateLine;
            }else if (c == -1){
                time = productDate;
            }


            Date date = new Date();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.DATE,1);
            date.setTime(calendar.getTimeInMillis());
            String today = sdf.format(date);
            c = DateUtil.compare(time,today);
            if (c == 1){
                time  = today;
            }else if (c == -1){

            }


            if (time != null && !"".equals(time)) {
                long timeMill = sdf.parse(time).getTime();
                calendar.setTimeInMillis(timeMill);

                int sy = calendar.get(Calendar.YEAR);
                int sm = calendar.get(Calendar.MONTH) + 1;
                int sd = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
                calendar.add(Calendar.MONTH,48);
                int ey = calendar.get(Calendar.YEAR);
                int em = calendar.get(Calendar.MONTH) + 1;
                int ed = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setEndYear(ey);
                timeDialog.setEndMonth(em);
                timeDialog.setEndDay(ed);
                if (ey < sy) {
                    sy = ey;
                }
                if (sy == ey && em < sm) {
                    sm = em;
                }
                if (sy == ey && em == sm && ed < sd) {
                    sd = ed;
                }
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);

            } else {
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.DATE,1);
                int sy = calendar.get(Calendar.YEAR);
                int sm = calendar.get(Calendar.MONTH) + 1;
                int sd = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);

                calendar.add(Calendar.MONTH,24);
                int ey = calendar.get(Calendar.YEAR);
                int em = calendar.get(Calendar.MONTH) + 1;
                int ed = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setEndYear(ey);
                timeDialog.setEndMonth(em);
                timeDialog.setEndDay(ed);
                if (ey < sy) {
                    sy = ey;
                }
                if (sy == ey && em < sm) {
                    sm = em;
                }
                if (sy == ey && em == sm && ed < sd) {
                    sd = ed;
                }
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
            }



        } catch (ParseException e) {
            e.printStackTrace();
        }

    }


    private void openDateline(String time) {

        TimePickerDialog timeDialog = new TimePickerDialog(this);


        timeDialog.setTitle("预售截止时间");

        timeDialog.setOnPositiveClickListener(new TimePickerDialog.OnPositiveClickListener() {
            @Override
            public void onPositiveClickListener(int index, String data) {
                ciProductDateLine.setContentText(data);
            }
        });

        if ("1".equals(type)) {//现货

        }else {//预售
            calculateProductDataLine(timeDialog);

        }
        timeDialog.setCurrentDate(time);

        timeDialog.show();
    }

    private void openProductDate(String time) {
        TimePickerDialog timeDialog = new TimePickerDialog(this);




        timeDialog.setOnPositiveClickListener(new TimePickerDialog.OnPositiveClickListener() {
            @Override
            public void onPositiveClickListener(int index, String data) {
                ciProductDate.setContentText(data);
            }
        });
        if ("1".equals(type)) {//现货
            calculateProductTime(timeDialog);
            timeDialog.setTitle("进货日期");
        }else {//预售
            calculatePreProductTime(timeDialog);
            timeDialog.setTitle("生产日期");
        }
        timeDialog.setCurrentDate(time);

        timeDialog.show();
    }

    //现货生产日期约束
    private void calculateProductTime(TimePickerDialog timeDialog) {

        String arrivalDate = ciProductInDate.getContentText();

        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            //截止时间
            if (arrivalDate != null && !"".equals(arrivalDate)) {
                long timeMill = sdf.parse(arrivalDate).getTime();
                calendar.setTimeInMillis(timeMill);
            } else {
                calendar.setTimeInMillis(System.currentTimeMillis());
            }
            int ey = calendar.get(Calendar.YEAR);
            int em = calendar.get(Calendar.MONTH) + 1;
            int ed = calendar.get(Calendar.DAY_OF_MONTH);
            timeDialog.setEndYear(ey);
            timeDialog.setEndMonth(em);
            timeDialog.setEndDay(ed);
            calendar.add(Calendar.MONTH,-24);
            int sy = calendar.get(Calendar.YEAR);
            int sm = calendar.get(Calendar.MONTH) + 1;
            int sd = calendar.get(Calendar.DAY_OF_MONTH);
            timeDialog.setStartYear(sy);
            timeDialog.setStartMonth(sm);
            timeDialog.setStartDay(sd);
            if (ey < sy) {
                sy = ey;
            }
            if (sy == ey && em < sm) {
                sm = em;
            }
            if (sy == ey && em == sm && ed < sd) {
                sd = ed;
            }
            timeDialog.setStartYear(sy);
            timeDialog.setStartMonth(sm);
            timeDialog.setStartDay(sd);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
    //预售生产日期约束
    private void calculatePreProductTime(TimePickerDialog timeDialog) {

        String arrivalDate = ciProductInDate.getContentText();

        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            //截止时间
            if (arrivalDate != null && !"".equals(arrivalDate)) {
                long timeMill = sdf.parse(arrivalDate).getTime();
                calendar.setTimeInMillis(timeMill);
                int ey = calendar.get(Calendar.YEAR);
                int em = calendar.get(Calendar.MONTH) + 1;
                int ed = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setEndYear(ey);
                timeDialog.setEndMonth(em);
                timeDialog.setEndDay(ed);
                calendar.add(Calendar.MONTH,-24);
                int sy = calendar.get(Calendar.YEAR);
                int sm = calendar.get(Calendar.MONTH) + 1;
                int sd = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
                if (ey < sy) {
                    sy = ey;
                }
                if (sy == ey && em < sm) {
                    sm = em;
                }
                if (sy == ey && em == sm && ed < sd) {
                    sd = ed;
                }
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
            } else {
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.MONTH,24);
                int ey = calendar.get(Calendar.YEAR);
                int em = calendar.get(Calendar.MONTH) + 1;
                int ed = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setEndYear(ey);
                timeDialog.setEndMonth(em);
                timeDialog.setEndDay(ed);
                calendar.add(Calendar.MONTH,-48);
                int sy = calendar.get(Calendar.YEAR);
                int sm = calendar.get(Calendar.MONTH) + 1;
                int sd = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
                if (ey < sy) {
                    sy = ey;
                }
                if (sy == ey && em < sm) {
                    sm = em;
                }
                if (sy == ey && em == sm && ed < sd) {
                    sd = ed;
                }
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
    //预售截止期约束
    private void calculateProductDataLine(TimePickerDialog timeDialog) {

        String arrivalDate = ciProductInDate.getContentText();

        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            //截止时间
            if (arrivalDate != null && !"".equals(arrivalDate)) {
                long timeMill = sdf.parse(arrivalDate).getTime();
                calendar.setTimeInMillis(timeMill);
                int ey = calendar.get(Calendar.YEAR);
                int em = calendar.get(Calendar.MONTH) + 1;
                int ed = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setEndYear(ey);
                timeDialog.setEndMonth(em);
                timeDialog.setEndDay(ed);


                calendar.setTimeInMillis(System.currentTimeMillis());
                int sy = calendar.get(Calendar.YEAR);
                int sm = calendar.get(Calendar.MONTH) + 1;
                int sd = calendar.get(Calendar.DAY_OF_MONTH);

                if (ey < sy) {
                    sy = ey;
                }
                if (sy == ey && em < sm) {
                    sm = em;
                }
                if (sy == ey && em == sm && ed < sd) {
                    sd = ed;
                }
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
                Log.e("time","sy:"+sy+"--sm:"+sm+"--sd:"+sd);
                Log.e("time","ey:"+ey+"--em:"+em+"--ed:"+ed);

            } else {
                calendar.setTimeInMillis(System.currentTimeMillis());
                int sy = calendar.get(Calendar.YEAR);
                int sm = calendar.get(Calendar.MONTH) + 1;
                int sd = calendar.get(Calendar.DAY_OF_MONTH);

                calendar.add(Calendar.MONTH,24);
                int ey = calendar.get(Calendar.YEAR);
                int em = calendar.get(Calendar.MONTH) + 1;
                int ed = calendar.get(Calendar.DAY_OF_MONTH);
                timeDialog.setEndYear(ey);
                timeDialog.setEndMonth(em);
                timeDialog.setEndDay(ed);


                if (ey < sy) {
                    sy = ey;
                }
                if (sy == ey && em < sm) {
                    sm = em;
                }
                if (sy == ey && em == sm && ed < sd) {
                    sd = ed;
                }
                timeDialog.setStartYear(sy);
                timeDialog.setStartMonth(sm);
                timeDialog.setStartDay(sd);
                Log.e("time","--sy:"+sy+"--sm:"+sm+"--sd:"+sd);
                Log.e("time","--ey:"+ey+"--em:"+em+"--ed:"+ed);

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        photoCoreDetail.onResult(requestCode, resultCode, data);
    }



    private OnRequestListener<StatusEntity> onPurchaseAddRequestListener = new OnRequestListener<StatusEntity>() {
        @Override
        public StatusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StatusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {

            hideProgressDialog();
            StatusEntity responseElement = response.getResponseElement();
            String result = responseElement.getResult();
            if ("success".equals(result)){
                toast("添加成功");
                setResult(0x001);
                finish();
            }else {
                toast("添加进货信息失败");
            }


            /*
            StatusEntity responseElement = response.getResponseElement();
            if (responseElement.isSuccess()) {
                toast("添加成功");
                finish();
            } else {
                //没有数据
                toast("添加进货信息失败");

            }*/

        }
    };



}
